<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Plugin\Attractions\AttractionManagement\Model\AttractionUser;

class AttractionUserRepository extends IRepository
{
    public function __construct(protected readonly AttractionUser $model) {}

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query
            ->when(Arr::get($params, 'id'), static function (Builder $query, $id) {
                $query->whereIn('id', Arr::wrap($id));
            })
            ->when(Arr::get($params, 'username'), static function (Builder $query, $username) {
                $query->where('username', 'like', "%{$username}%");
            })
            ->when(Arr::get($params, 'times'), static function (Builder $query, $times) {
                $query->where('times', '>=', $times);
            });
    }

    public function list(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->with(['bookings:id,info_id as infoId', 'touristInfos:id,name,phone,id_card as idCard'])
            ->get(['id', 'username', 'avatar', 'times']);
    }

    public function showListPage(array $params = []): array
    {
        $page = Arr::get($params, 'page', 1); 
        $pageSize = Arr::get($params, 'pageSize', 30);
        return $this->perQuery($this->getQuery(), $params)
            ->simplePaginate($pageSize, ['*'], 'page', $page)
            ->items();
    }
}